/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.playground.core.service.impl;

import com.playground.core.entity.LoggedPlayer;
import com.playground.core.entity.Player;
import com.playground.core.entity.PlayerStatus;
import com.playground.core.repository.LoggedPlayerRepository;
import com.playground.core.repository.PlayerRepository;
import com.playground.core.service.LoginService;
import java.util.Date;
import javax.inject.Inject;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Service;

/**
 *
 * @author Tiago Peres
 */
@Service("loginService")
public class LoginServiceImpl implements LoginService {
    private static long timeout = 1800000;
    
    @Inject
    private LoggedPlayerRepository loggedPlayerRepository;
    
    @Inject
    private PlayerRepository playerRepository;
    
    

    public LoggedPlayer login(long playerId) {
        System.out.println("entered service");
        if (null == playerRepository) System.out.println("!!! player repository is null");
        else System.out.println("+++ player repository is not null");
        try{
            Player p = playerRepository.findOne(playerId);
        
            System.out.println("database executed");
            if (null != p){
                LoggedPlayer lp = new LoggedPlayer(playerId, "A2377789645642325vgHDEhAasS445");
                lp.setFacebookId(p.getFacebookId());
                lp.setLastAction(new Date());
                lp.setNickname(p.getNickname());
                lp.setStatus(PlayerStatus.online);
                return loggedPlayerRepository.save(lp);
            }
        } catch (Exception e){
            System.out.println("WTF? AN EXCEPTION!");
            //Logger.getLogger(LoginServiceImpl.class).log(Logger.Level.FATAL, e.g);
            e.printStackTrace();
        }
        return null;
    }

    public void login(String facebookId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public LoggedPlayer verifyLoggedPlayerKey(long playerId, String key) {
        System.out.println("searching for logged player");
        LoggedPlayer lp = loggedPlayerRepository.findOne(playerId);
        if (null != lp && key.equals(lp.getKey())){
            System.out.println("player found");
            if (lp.getLastAction().getTime() + timeout > new Date().getTime()){
                System.out.println("time is valid");
                return lp;
            } else{
                System.out.println("timeout");
                loggedPlayerRepository.delete(lp);
            }
        }else{
        System.out.println("player was not found");}
        return null;
    }

    public void logout(LoggedPlayer loggedPlayer) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
